package com.redstars.generate.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.entity.SysGenerateTemplate;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.service.SysProjectConfigService;
import com.redstars.generate.service.SysTableService;
import com.redstars.generate.service.SysTemplateVersionService;
import com.redstars.generate.utils.PageUtils;
import com.redstars.generate.vo.SysProjectConfigVo;
import com.redstars.generate.vo.SysTableVo;
import lombok.AllArgsConstructor;
import com.redstars.generate.convert.SysGenerateTemplateConvert;
import com.redstars.generate.query.SysGenerateTemplateQuery;
import com.redstars.generate.vo.SysGenerateTemplateVo;
import com.redstars.generate.dao.SysGenerateTemplateDao;
import com.redstars.generate.service.SysGenerateTemplateService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 项目模板配置
 *
 * @author zhouhx
 * @since 1.0.0 2022-10-14
 */
@Service
@AllArgsConstructor
public class SysGenerateTemplateServiceImpl extends ServiceImpl<SysGenerateTemplateDao, SysGenerateTemplate> implements SysGenerateTemplateService {
    private final SysProjectConfigService configService;
    private final SysTableService tableService;

    @Override
    public PageResult<SysGenerateTemplateVo> page(SysGenerateTemplateQuery query) {
        LambdaQueryWrapper<SysGenerateTemplate> wrapper = getWrapper(query);
        Long templateVersionId = query.getTemplateVersionId();
        if(ObjectUtil.isEmpty(templateVersionId)){
            // 模板id不存在不允许查询
            return null;
        }
        PageUtils<SysGenerateTemplate> pageUtils = new PageUtils<>();
        IPage<SysGenerateTemplate> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page, SysGenerateTemplateVo.class);
    }

    private LambdaQueryWrapper<SysGenerateTemplate> getWrapper(SysGenerateTemplateQuery query) {
        LambdaQueryWrapper<SysGenerateTemplate> wrapper = Wrappers.lambdaQuery();
        if(ObjectUtil.isNotEmpty(query.getTemplateName())){
            wrapper.like(SysGenerateTemplate::getTemplateName,query.getTemplateName());
        }
        if(ObjectUtil.isNotEmpty(query.getTemplateVersionId())){
            wrapper.eq(SysGenerateTemplate::getTemplateVersionId,query.getTemplateVersionId());
        }
        if(ObjectUtil.isNotEmpty(query.getGroupCode())){
            wrapper.eq(SysGenerateTemplate::getGroupCode,query.getGroupCode());
        }
        wrapper.orderByAsc(SysGenerateTemplate::getGroupCode);
        wrapper.orderByAsc(SysGenerateTemplate::getCreateTime);
        return wrapper;
    }

    @Override
    public void save(SysGenerateTemplateVo vo) {
        SysGenerateTemplate entity = SysGenerateTemplateConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysGenerateTemplateVo vo) {
        SysGenerateTemplate entity = SysGenerateTemplateConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public SysGenerateTemplateVo detail(Long id) {
        SysGenerateTemplate detail = this.getById(id);
        if (ObjectUtil.isEmpty(detail)) {
            throw new RedstarsException("记录不存在");
        }
        return BeanUtil.copyProperties(detail, SysGenerateTemplateVo.class);
    }

    @Override
    public List<SysGenerateTemplateVo> list(SysGenerateTemplateQuery query) {
        LambdaQueryWrapper<SysGenerateTemplate> wrapper = getWrapper(query);
        List<SysGenerateTemplate> templates = list(wrapper);
        return SysGenerateTemplateConvert.INSTANCE.convertList(templates);
    }

    @Override
    public List<SysGenerateTemplateVo> getSelectTemplate() {
        SysProjectConfigVo configVo = configService.detail(1L);
        Long templateVersion = configVo.getTemplateVersion();
        String templateSelect = configVo.getTemplateSelect();
        if(ObjectUtil.isNotEmpty(templateSelect)){
            LambdaQueryWrapper<SysGenerateTemplate> queryWrapper = new QueryWrapper<SysGenerateTemplate>().lambda().in(SysGenerateTemplate::getId,templateSelect.split(","))
                    .eq(SysGenerateTemplate::getTemplateVersionId,templateVersion);
            List<SysGenerateTemplate> templates = this.list(queryWrapper);
            return SysGenerateTemplateConvert.INSTANCE.convertList(templates);
        }
        return null;
    }

    @Override
    public List<SysGenerateTemplateVo> getSelectTemplate(Long tableId) {
        SysTableVo tableVo = tableService.detail(tableId);
        if(ObjectUtil.isNotEmpty(tableVo)){
            SysProjectConfigVo configVo = configService.detailByProjectId(tableVo.getSysProjectId());
            Long templateVersion = configVo.getTemplateVersion();
            String templateSelect = configVo.getTemplateSelect();
            if(ObjectUtil.isNotEmpty(templateSelect)){
                LambdaQueryWrapper<SysGenerateTemplate> queryWrapper = new QueryWrapper<SysGenerateTemplate>().lambda().in(SysGenerateTemplate::getId,templateSelect.split(","))
                        .eq(SysGenerateTemplate::getTemplateVersionId,templateVersion);
                List<SysGenerateTemplate> templates = this.list(queryWrapper);
                return SysGenerateTemplateConvert.INSTANCE.convertList(templates);
            }
        }
        return null;
    }

    @Override
    public void removeByTemplateVersion(Long versionId) {
        LambdaQueryWrapper<SysGenerateTemplate> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(SysGenerateTemplate::getTemplateVersionId,versionId);
        remove(queryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void copyTemplate(Long versionId,List<Long> templateIds) {
        if(ObjectUtil.isEmpty(versionId)){
            throw new RedstarsException("模板版本id为空");
        }
        if(CollUtil.isEmpty(templateIds)){
            throw new RedstarsException("要复制的模板id集合为空");
        }
        templateIds.forEach(templateId ->{
            SysGenerateTemplateVo detail = this.detail(templateId);
            detail.setId(null);
            detail.setTemplateVersionId(versionId);
            save(detail);
        });
    }

}